package resoft.jeasyform.event;

import charva.awt.event.KeyEvent;
import charva.awt.event.KeyAdapter;
import resoft.jeasyform.components.InputField;

/**
 * <p>fixed length</p>
 * User: albert
 * Date: Aug 28, 2007
 * Time: 12:40:53 AM
 */
public class FixedLengthEventHandler extends KeyAdapter {
    public FixedLengthEventHandler(InputField inputField,int maxLen) {
        this.inputField = inputField;
        this.maxLen = maxLen;
    }
    /**
     * KEY_PRESSED events are fired when any key (including a function
     * key and cursor key) is pressed while the component has keyboard
     * input focus.
     * KeyEvent.getKeyCode() can be used to find out which key was pressed.
     */
    public void keyPressed(KeyEvent keyEvent) {
        String value = inputField.getValue();

        if(maxLen <=0 || value.length()< maxLen) {
            return;
        }
        int keyCode = keyEvent.getKeyCode();
        if (keyCode != KeyEvent.VK_ENTER && keyCode != '\t' && keyCode!=KeyEvent.VK_ESCAPE) {
            keyEvent.consume();
        }
    }
    private InputField inputField;
    private int maxLen;
}
